package cn.phecda.boot.tenant.test.database.mybatis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p> TODO </p>
 *
 * @author : wanpinwei
 */
@Service
@RequiredArgsConstructor
public class PersonMybatisService {
    private final PersonMybatisMapper personMyBatisMapper;

    @Async
    @Transactional
    @SneakyThrows
    public void asyncSave(String name) {
        TimeUnit.SECONDS.sleep(3);
        personMyBatisMapper.insert(personMyBatisMapper.named(name));
    }

    @Transactional
    public PersonMybatisEntity save(String name) {
        PersonMybatisEntity person = personMyBatisMapper.named(name);
        personMyBatisMapper.insert(person);
        return person;
    }

    @Transactional
    public void update(String id, String name) {
        PersonMybatisEntity person = personMyBatisMapper.selectById(id);
        person.setName(name);
        personMyBatisMapper.updateById(person);
    }

    public List<PersonMybatisEntity> findAll() {
        return personMyBatisMapper.selectList(new QueryWrapper<>());
    }

    public PersonMybatisEntity findById(String id) {
        return personMyBatisMapper.selectById(id);
    }

    @Transactional
    public void delete(String id) {
        personMyBatisMapper.deleteById(id);
    }
}
